package cn.onein.edu.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.ActionCategory;
import cn.onein.edu.biz.bean.Product;
import cn.onein.edu.biz.bean.User;
import cn.onein.edu.biz.bean.UserTemp;
import cn.onein.edu.common.ProductType;
import cn.onein.edu.web.dao.UserDao;
import cn.onein.edu.web.service.UserService;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	
	@Override
	public Long getActionCategoryId(Integer age,Boolean sex) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("age", age);
		map.put("sex", sex);
		ActionCategory category = userDao.getCategoryByAgeAndSex(map);
		if(null == category){
			category = userDao.getCategoryByAge(age);
		}
		
		return null == category ? null : category.getId();
	}

	@Override
	public void saveRegisterUser(User newUser) {
		userDao.saveRegisterUser(newUser);
		User user = userDao.getUserByEmail(newUser.getEmail());
		
		userDao.insertInitUserActionForNewUser(user);
	}

	@Override
	public Boolean getEmail(String email) {
		User user = userDao.getUserByEmail(email);
		
		return null == user ? false : true;
	}
	
	@Override
	public User getUserByEmail(String email) {
		User user = userDao.getUserByEmail(email);
		
		return user;
	}

	@Override
	public User getUserByName(String name) {
		User user = userDao.getUserByName(name);
		
		return user;
	}

	@Override
	public void updateUser(User user,UserTemp userTemp) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("id", user.getId());
		param.put("name", user.getName());
		param.put("qq", user.getQq());
		param.put("nickName", user.getNickName());
		param.put("mobile", user.getMobile());
		
		userDao.updateUser(param,userTemp);
	}

	@Override
	public User getUserById(Long id) {
		return userDao.getUserById(id);
	}

	@Override
	public void updateUserPassword(Long id,String password) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("password", password);
		param.put("id", id);
		
		userDao.updateUserPassword(param);
	}

	@Override
	public void updateUserExtendInfo(User user,User oldUser) {
		userDao.updateUserExtendInfo(user);
		
		if(!user.getBirthday().equals(oldUser.getBirthday())){
			userDao.updateUserActionByBirthday(user, oldUser);
		}
	}

	@Override
	public void saveUserTemp(UserTemp userTemp) {
		userDao.saveUserTemp(userTemp);
		
	}
	
	public UserTemp getUserTempByUUID(String uuid){
		return userDao.getUserTempByUUID(uuid);
	}

	@Override
	public void updateUserEmailByUserId(String uuid,String email, Long userId) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		param.put("email", email);
		param.put("userId", userId);
		
		userDao.updateUserEmailByUserId(param);
		
		userDao.deleteUserTempByUUID(uuid);
	}

	@Override
	public List<Product> getProductByType(ProductType pType) {
		return userDao.getProductByType(pType);
	}
	
}
